package com.atguigu.daijia.order.handle;

import com.atguigu.daijia.order.service.OrderInfoService;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import jakarta.annotation.PostConstruct;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description 监听延迟队列
 * @Author dingchunlin
 * @Date 2024/09/12 16:20
 * @Version 1.0.0
 */
@Component
public class RedisDelayHandle {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private OrderInfoService orderInfoService;


    @PostConstruct
    public void listener(){
        new Thread(() -> {
            while (true){
                // 获取到设置的延迟队列里面的阻塞队列
                RBlockingDeque<String> blockingDeque =
                        redissonClient.getBlockingDeque("queue_cancel");
                // 从队列中获取消息
                try {
                    String orderId = blockingDeque.take();
                    if (StringUtils.isNotEmpty(orderId)){
                        // 调用订单服务取消订单
                        orderInfoService.orderCancel(Long.parseLong(orderId));
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }



}
